// pages/login/login.js
const common = require('../../utils/common.js');
const app = getApp()
var that = "";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    codeText: '发送验证码',
    codeIsClick: true,
    code: ''
  },
  userLogin: function () {
    // console.log(this.data.phone);
    //登录
    // this.getLogin(this.data.phone);
    that.toLogin();
  },
  //发送验证码
  sendCode: function () {
    if (!common.checkMobile(this.data.phone)) {
      wx.showToast({
        title: '手机号码错误',
        image: '../../image/shibai.png'
      })
      return;
    }
    common._get_no('/wx/landlord/login/sendCode', { mobile: this.data.phone },
      //成功
      function (res) {
        console.log(res)
        if (res.data.success) {
          wx.showToast({
            title: res.data.msg,
          })
          that.countDown();
        }
      });
  },
  //登录
  toLogin: function () {
    if (this.data.code == '') {
      wx.showToast({
        title: '请输入验证码',
        image: '../../image/shibai.png'
      })
    }
    var utoken = wx.getStorageSync('utoken');
    console.log('utoken')
    console.log(utoken)
    common._get_no('/wx/landlord/login', { mobile: this.data.phone, validCode: this.data.code, utoken: wx.getStorageSync('utoken') },
      //成功
      function (res) {
        console.log('登录返回测试');
        console.log(res.data);
        if (res.data.success) {
          wx.showToast({
            title: '登录成功',
          })
          //清除登录
          wx.removeStorageSync('utoken')
          //刷新首页以及登录页
          wx.setStorageSync('refresh', 'refresh');
          wx.setStorageSync('result', 'result');
          app.globalData.isLogin = true;
          //登录
          wx.setStorageSync('token', res.data.token);
          that.setData({
            isLogon: true
          })
          app.globalData.isLogin = true;

          //延时两秒关闭当前页面
          setTimeout(function () {
            wx.navigateBack();
          }, 2000)
        }
      });
    
  },
  //倒计时
  countDown: function () {
    var time = 60;
    // that.data.codeText = time + "秒后重新发送";
    this.setData({
      codeIsClick: false
    })

    var interval = setInterval(function () {
      that.setData({
        codeText: time + "秒后重新发送",
      })
      time--;
      if (time < 0) {
        that.setData({
          codeText: "重新发送",
          codeIsClick: true
        })
        clearInterval(interval);
      }else{
        that.setData({
          codeIsClick: false
        })
      }
    }, 1000)
  },
  //获取input输入的内容
  phoneFc: function (e) {
    this.setData({
      phone: e.detail.value,
    })
  },
  //输入验证码
  inputCode: function (e) {
    this.setData({
      code: e.detail.value,
    })
  },

  //登录
  aLogin: function (params) {
    _post_form_no('/wx/landlord/login', params,
      //成功
      function (res) {
        if (res.data.success) {
          wx.setStorageSync('refresh', 'refresh');
          wx.setStorageSync('result', 'result');
          wx.setStorageSync('token', res.data.token);
          app.globalData.isLogin = true;
          //延时两秒关闭当前页面
          setTimeout(function () {
            wx.navigateBack();
          }, 2000)
        } else {
          if (res.data.token && res.data.token != '') {
            wx.showToast({
              title: res.data.msg,
              image: '../../image/shibai.png'
            })
            wx.setStorageSync('utoken', res.data.token);
          }
        }
      });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    // common.autoLogin(this.aLogin);
    //已经登录成功的用户直接自动登录
    //  var phone = wx.getStorageSync('phone');
    //  if (phone){66
    //  }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 登录
  getLogin: function (phone) {
    wx.showLoading({
      title: '提交中...',
    })

    var that = this;
    wx.request({
      url: app.globalData.url + '/wx/landlord/login',
      data: 'mobile=' + phone,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "POST",
      success: function (res) {
        wx.hideLoading();
        console.log(res);
        if (!res.data.success) {
          if (res.data.msg) {
            wx.showToast({
              title: res.data.msg,
              image: '../../image/shibai.png',
              duration: 3000
            })
          } else {
            wx.showToast({
              title: res.statusCode + '',
              image: '../../image/shibai.png',
              duration: 3000
            })
          }

        } else if (res.data.success) {
          //登入成功把token保存到本地
          wx.setStorageSync('token', res.data.token);
          //把手机缓存到本地
          wx.setStorageSync('phone', phone);
          //通知首页和房源页面更新
          wx.setStorageSync('refresh', 'refresh');
          wx.setStorageSync('result', 'result');

          app.globalData.isLogin = true;
          wx.showToast({
            title: '登录成功',
            image: '',
          })
          //延时两秒关闭当前页面
          setTimeout(function () {
            wx.navigateBack();
          }, 2000)


        }
        // wx.hideLoading();
      },
      fail: function () {
        wx.showToast({
          title: '登录失败',
          image: '../../image/shibai.png',
        })
      }
    })

  },


})